package com.rawchen.util.markdown.ext.heimu.internal;

import com.rawchen.util.markdown.ext.heimu.Heimu;
import org.commonmark.node.Node;
import org.commonmark.parser.delimiter.DelimiterProcessor;
import org.commonmark.parser.delimiter.DelimiterRun;

// TODO 尝试利用已有DelimiterProcessor实现类重写该类

/**
 * @Description: 定界
 */
public class HeimuDelimiterProcessor implements DelimiterProcessor {
	@Override
	public char getOpeningCharacter() {
		return '@';
	}

	@Override
	public char getClosingCharacter() {
		return '@';
	}

	@Override
	public int getMinLength() {
		return 2;
	}

	@Override
	public int process(DelimiterRun openingRun, DelimiterRun closingRun) {
		// Wrap nodes between delimiters in heimu.
		Node heimu = new Heimu();
		Node tmp = openingRun.getOpener().getNext();
		while (tmp != null && tmp != closingRun.getCloser()) {
			Node next = tmp.getNext();
			heimu.appendChild(tmp);
			tmp = next;
		}
		openingRun.getOpener().insertAfter(heimu);


		if (openingRun.length() >= 2 && closingRun.length() >= 2) {
			// Use exactly two delimiters even if we have more, and don't care about internal openers/closers.
			return 2;
		} else {
			return 0;
		}
	}
}
